package com.example.spirngbootdemo.bll.impl;

import com.example.spirngbootdemo.annotation.LogTrack;
import com.example.spirngbootdemo.bll.ITestBll;
import com.example.spirngbootdemo.db.UserService;
import com.example.spirngbootdemo.utils.ThreadTaskUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.FutureTask;

/**
 * Description:
 *
 * @author Yang
 * create time 2021-10-18 19:21
 * @since 0.0.1
 */
@Service
@Slf4j
public class TestBll implements ITestBll {

    @Autowired
    private UserService userService;

    @Override
    public String getData(String data) {

        log.info("TestBll getData");
        return userService.getData(data);
    }

    @Override
    public void log() {
        log.info("==============================================");
        ThreadTaskUtils.run(() -> run());
        FutureTask<String> futureTask = new FutureTask<String>(() -> call());
        ThreadTaskUtils.run(futureTask);
        try {
            log.info("===================: {}", futureTask.get());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        log.info("==============================================");
    }

    /**
     * 使用注解的方式实现日志的链路跟踪
     */
    @Override
    @LogTrack
    public void log2() {
        log.info("==============================================");
        ThreadTaskUtils.run(() -> run());
        FutureTask<String> futureTask = new FutureTask<String>(() -> call());
        ThreadTaskUtils.run(futureTask);
        try {
            log.info("===================: {}", futureTask.get());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        log.info("==============================================");
    }

    private String call() {
        log.info("11111111111");
        return "3333";
    }

    public void run() {
        log.info("222222222222222");
    }
}
